package com.example.chcrm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.chcrm.entity.common.*;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.entity.employee.EssEmployee;
import com.example.chcrm.mapper.EssEmployeeMapper;
import com.example.chcrm.mapper.ResMenuMapper;
import com.example.chcrm.mapper.RoleMenuMapper;
import com.example.chcrm.service.EssRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

/**
 * @Author 邹
 * @Date 2025/4/30 11:40
 * @Version 1.0
 */
@Service
public class EssRoleMenuServiceImpl<T> implements EssRoleMenuService<T>  {
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private EssEmployeeMapper employeeMapper;
    @Autowired
    private ResMenuMapper resMenuMapper;
    @Override
    public ResponseMsg<List<Object>> seAllMenu(EmployeeVo empVo1) {
        ResponseMsg<List<Object>> resp = new ResponseMsg<>();
        String username = empVo1.getEmpNm();
        EssEmployee empVo = employeeMapper.selectOne(new QueryWrapper<EssEmployee>().eq("emp_name",username));
        if (empVo ==null){
            resp.fail("未查询到用户");
            return  resp;
        }
        List<ResMenu> data =null;
        if (username.equals("超级管理员")){
            data = resMenuMapper.selectList(new QueryWrapper<ResMenu>().orderByAsc("sortOrder"));
        }else {
            data = roleMenuMapper.seAllMenu(empVo.getId());
        }
        List<MenuVo> list = new ArrayList<>();
        for (ResMenu resMenu : data) {
            MenuVo menuVo = new MenuVo();
            menuVo.setId(resMenu.getResource());
            menuVo.setPid(resMenu.getGroupResource());
            menuVo.setTitle(resMenu.getLabel());
            menuVo.setType(resMenu.getIsmenu());
            menuVo.setCen(resMenu.getHierarchy());
            menuVo.setMenuId(resMenu.getId());
            list.add(menuVo);
        }
        MenuTreeUtil menuTree = new MenuTreeUtil();
        List<Object> menuList = menuTree.menuList(list);
        resp.setData(menuList);
        resp.setMessage("菜单查询成功！");
        resp.setStatus(0);

        return  resp;
    }

    @Override
    public List<Object> getMenuByRoleId(String roleId) {
        List<MenuVo> mList = new ArrayList<>();
        List<Model_MenuVo> data = roleMenuMapper.seMenuByRoleIds(roleId);
        for (Model_MenuVo mm : data) {
            if (mm.getLabel() != null) {
                MenuVo mun = new MenuVo();
                mun.setTitle(mm.getLabel());
                mun.setType(mm.getIsmenu());
                mun.setCen(mm.getHierarchy());
                mun.setId(mm.getResource());
                mun.setPid(mm.getGroupresource());
                mun.setMenuId(mm.getId());
                mList.add(mun);
            }
        }
        MenuTreeUtil menuTree = new MenuTreeUtil();
        List<Object> menuList = menuTree.menuList(removeDuplicate(mList));
        return menuList;
    }

    public   static  List<MenuVo>  removeDuplicate(List<MenuVo> list)  {
        for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {
            for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {
                if  (list.get(j).getId().equals(list.get(i).getId() ))  {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    @Override
    public void deletesRoleMenuById(String[] roleId) {
        roleMenuMapper.deletesRoleMenuById(roleId);
    }

    @Override
    public void setRoleResource(List<EssRoleMenu> list) {
        List <String> resMenusIds = list.stream().map(EssRoleMenu::getMenuId).collect(Collectors.toList());
        List<ResMenu>resMenuList = resMenuMapper.selectList(null,new QueryWrapper<ResMenu>()
                .in("resource",resMenusIds));
        ConcurrentMap<String,String> resMenuIds = new ConcurrentHashMap<>();
        for (ResMenu res :resMenuList){
            resMenuIds.put(res.getResource(),res.getId());
        }
        for (EssRoleMenu res :list){
            String menuId = resMenuIds.get(res.getMenuId());
            res.setMenuId(menuId);
        }
        roleMenuMapper.setRoleResource(list);
    }
}
